package com.ugoodtech.mdcc.web.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class JudgementUtils {

	/**
	 * Check if these query parameters has a need of query. If a parameter of list type has null
	 * first
	 */
	public static Boolean isNoQuery(List<?>... queries) {
		List<?> queryAfterFilter = null;
		for (List<?> query : queries) {
			// Pass this round when query is null.
			if (query == null) {
				continue;
			}
			queryAfterFilter = query.stream().filter(Objects::nonNull).collect(Collectors.toList());
			// If query parameter has null element, set query to queryAfterFilter.
			// Then test size of queryAfterFilter, if it's 0 and it contains null element originally,
			// it denotes that this parameter do not need to query.
			if (query.contains(null)) {
				query = queryAfterFilter;
				if (queryAfterFilter.size() == 0) {
					continue;
				}
			}
			return false;
		}
		return true;
	}

	public static Boolean isNoQuery(Object... objects) {
		for (Object o : objects) {
			if (!Objects.isNull(o)) {
				return false;
			}
		}
		return true;
	}

	public static Boolean isAllEmpty(List<?>... lists) {
		for (List<?> list : lists) {
			if (list == null) {
				return false;
			} else {
				if (list.size() != 0) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * Check if any list in parameters is empty for query judgement. if a list contains {@code null},
	 * it means that this list is not empty.
	 */
	public static Boolean isAnyEmpty(List<?>... lists) {
		for (List<?> list : lists) {
			if (list != null && list.size() == 0) {
				return true;
			}
		}
		return false;
	}

	public static Boolean isNoQuery(String... queries) {
		for (String query : queries) {
			if (query != null && query.length() != 0) {
				return false;
			}
		}
		return true;
	}

	public static List convertListToNull(List<?> list) {
		if (list == null) {
			return null;
		} else {
			if (org.apache.commons.collections4.CollectionUtils
					.isEmpty(CollectionUtils.removeNull(list))) {
				return null;
			} else {
				return list;
			}
		}
	}

	public static String convertStringToNull(String string) {
		if (StringUtils.isEmpty(string)) {
			return null;
		} else {
			return string;
		}
	}

}
